﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views {
    import flash.events.*;
    import org.robotlegs.core.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.views.components.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.utils.*;
    import net.wooga.pets.models.world.*;

    public class TreatmentProgressMediator extends Mediator implements IMediator, ITicking {

		[Inject]
        public var view:TreatmentProgressDisplay;
		[Inject]
        public var gameStateModel:GameStateModel;
		[Inject]
        public var treatmentProgressModel:TreatmentProgressDisplayModel;
		[Inject]
        public var worldModel:WorldModel;
		[Inject]
        public var roomSlotsModel:RoomSlotsModel;
		[Inject]
        public var userModel:UserModel;
		[Inject]
        public var languageModel:LanguageModel;
        private var _currentTreatmentFinishedInValue:Number;
        private var _currentToolTipTextPointer:String;

        private static const TOOLTIP_TEXT_LOOKUP_KEY_RUNNING:String = "meta_nexttreatment_tooltip";
        private static const TOOLTIP_TEXT_LOOKUP_KEY_PENDING:String = "meta_nexttreatment_ready_tooltip";
        private static const CLEAR_TOOL_TIP:String = null;

        override public function onRegister():void{
            eventMap.mapListener(eventDispatcher, BuildingEvent.FACADE_CHANGED, this.onBuildingFacadeStatusChanged);
            eventMap.mapListener(eventDispatcher, EventConstants.VISIT_FRIEND, this.onVisitFriendStatusChanged);
            this.setMappingsToEventMap(true);
            this.mapViewClickHandler();
            this.mapViewMouseHandlers();
        }
        private function setMappingsToEventMap(_arg1:Boolean):void{
            if (_arg1){
                eventMap.mapListener(eventDispatcher, TreatmentProgressMonitoringEvent.START_MONITORING_TREATMENT, this.onStartMonitoring, TreatmentProgressMonitoringEvent);
                eventMap.mapListener(eventDispatcher, TreatmentProgressMonitoringEvent.UPDATE_MONITORING_TREATMENT, this.onUpdateMonitoring, TreatmentProgressMonitoringEvent);
                eventMap.mapListener(eventDispatcher, TreatmentProgressMonitoringEvent.END_MONITORING_TREATMENT, this.onEndMonitoring, TreatmentProgressMonitoringEvent);
                eventMap.mapListener(eventDispatcher, TreatmentProgressMonitoringEvent.START_PROCESSING_PENDING_TREATMENTS, this.onPendingFinalisationStart, TreatmentProgressMonitoringEvent);
                eventMap.mapListener(eventDispatcher, TreatmentProgressMonitoringEvent.END_PROCESSING_PENDING_TREATMENTS, this.onPendingFinalisationFinish, TreatmentProgressMonitoringEvent);
                eventMap.mapListener(eventDispatcher, TreatmentProgressMonitoringEvent.NUM_PENDING_FINALISATION_UPDATE, this.onNumPendingUpdate, TreatmentProgressMonitoringEvent);
                eventMap.mapListener(eventDispatcher, TreatmentProgressMonitoringEvent.NO_TREATMENT_AVAILABLE, this.onNoTreatmentsAvailable, TreatmentProgressMonitoringEvent);
            } else {
                eventMap.unmapListener(eventDispatcher, TreatmentProgressMonitoringEvent.START_MONITORING_TREATMENT, this.onStartMonitoring, TreatmentProgressMonitoringEvent);
                eventMap.unmapListener(eventDispatcher, TreatmentProgressMonitoringEvent.UPDATE_MONITORING_TREATMENT, this.onUpdateMonitoring, TreatmentProgressMonitoringEvent);
                eventMap.unmapListener(eventDispatcher, TreatmentProgressMonitoringEvent.END_MONITORING_TREATMENT, this.onEndMonitoring, TreatmentProgressMonitoringEvent);
                eventMap.unmapListener(eventDispatcher, TreatmentProgressMonitoringEvent.START_PROCESSING_PENDING_TREATMENTS, this.onPendingFinalisationStart, TreatmentProgressMonitoringEvent);
                eventMap.unmapListener(eventDispatcher, TreatmentProgressMonitoringEvent.END_PROCESSING_PENDING_TREATMENTS, this.onPendingFinalisationFinish, TreatmentProgressMonitoringEvent);
                eventMap.unmapListener(eventDispatcher, TreatmentProgressMonitoringEvent.NUM_PENDING_FINALISATION_UPDATE, this.onNumPendingUpdate, TreatmentProgressMonitoringEvent);
                eventMap.unmapListener(eventDispatcher, TreatmentProgressMonitoringEvent.NO_TREATMENT_AVAILABLE, this.onNoTreatmentsAvailable, TreatmentProgressMonitoringEvent);
            };
        }
        private function onVisitFriendStatusChanged(_arg1:InfoEvent):void{
            var _local2:String = _arg1.id;
            this.treatmentProgressModel.flushPendingFinalisationPool();
            this.treatmentProgressModel.destroy();
            if (_local2 == this.userModel.mySocialID){
                this.setMappingsToEventMap(true);
            } else {
                this.view.hide();
                this.setMappingsToEventMap(false);
                this.destroy();
            };
            this.treatmentProgressModel.flushPendingFinalisationPool();
            this.treatmentProgressModel.destroy();
        }
        private function onBuildingFacadeStatusChanged(_arg1:BuildingEvent):void{
            if (this.gameStateModel.isFacadeVisible){
                this.unmapViewClickHandler();
            } else {
                this.mapViewClickHandler();
            };
        }
        private function onStartMonitoring(_arg1:TreatmentProgressMonitoringEvent):void{
            this.view.show();
            this.mapViewClickHandler();
            this.mapViewMouseHandlers();
            this.setNewFinishedInTimeToMonitor(_arg1.treatment.finishedIn);
            dispatch(new TickingEvent(TickingEvent.ADD_TICKING, this, TimeConstants.SECOND));
        }
        private function setNewFinishedInTimeToMonitor(_arg1:Number):void{
            this._currentTreatmentFinishedInValue = _arg1;
        }
        private function onUpdateMonitoring(_arg1:TreatmentProgressMonitoringEvent):void{
            this.view.show();
            this.setNewFinishedInTimeToMonitor(_arg1.treatment.finishedIn);
        }
        private function onEndMonitoring(_arg1:TreatmentProgressMonitoringEvent):void{
            dispatch(new TickingEvent(TickingEvent.REMOVE_TICKING, this));
        }
        private function onPendingFinalisationStart(_arg1:TreatmentProgressMonitoringEvent):void{
            this.view.startAlarmAnimation();
            this.changeTooltipText(TOOLTIP_TEXT_LOOKUP_KEY_PENDING);
        }
        private function onPendingFinalisationFinish(_arg1:TreatmentProgressMonitoringEvent):void{
            this.view.stopAlarmAnimation();
            this.changeTooltipText(TOOLTIP_TEXT_LOOKUP_KEY_RUNNING);
        }
        private function changeTooltipText(_arg1:String):void{
            var _local2:String;
            if (this.view.isSelected){
                _local2 = (this._currentToolTipTextPointer) ? this.languageModel.getTextByKey(this._currentToolTipTextPointer) : null;
                this.view.setTooltipData(_local2);
            };
            this._currentToolTipTextPointer = _arg1;
        }
        private function onNumPendingUpdate(_arg1:TreatmentProgressMonitoringEvent):void{
            this.view.updateNumTreatments(this.treatmentProgressModel.getNumPendingFinalisation);
        }
        private function onNoTreatmentsAvailable(_arg1:TreatmentProgressMonitoringEvent):void{
            this.view.hide();
            this.unmapViewMouseHandlers();
            this.unmapViewClickHandler();
        }
        private function unmapViewMouseHandlers():void{
            eventMap.unmapListener(this.view, MouseEvent.ROLL_OVER, this.onViewRollOver);
            eventMap.unmapListener(this.view, MouseEvent.ROLL_OUT, this.onViewRollOut);
        }
        private function mapViewMouseHandlers():void{
            eventMap.mapListener(this.view, MouseEvent.ROLL_OVER, this.onViewRollOver);
            eventMap.mapListener(this.view, MouseEvent.ROLL_OUT, this.onViewRollOut);
        }
        public function onTick(_arg1:Boolean=true, _arg2:Number=0):void{
            this._currentTreatmentFinishedInValue--;
            this.renderTimeToDisplay();
        }
        private function renderTimeToDisplay():void{
            var _local1:String = TimeUtils.secondsToHoursMinSec(this._currentTreatmentFinishedInValue);
            this.view.renderTimeToLabel(_local1);
        }
        private function mapViewClickHandler():void{
            if (!this.gameStateModel.isFacadeVisible){
                eventMap.mapListener(this.view, MouseEvent.MOUSE_DOWN, this.onViewClicked);
            };
        }
        private function unmapViewClickHandler():void{
            eventMap.unmapListener(this.view, MouseEvent.MOUSE_DOWN, this.onViewClicked);
        }
        private function onViewClicked(_arg1:MouseEvent):void{
            if (this.treatmentProgressModel.currentTreatmentAvailable()){
                this.zoomAndFlashRoomWithCurrentTreatment();
            };
        }
        private function zoomAndFlashRoomWithCurrentTreatment():void{
            var _local1:ITreatable;
            var _local2:IRoom;
            if (this.treatmentProgressModel.currentTreatmentAvailable()){
                _local1 = this.worldModel.getPatientByTreatmentId(this.treatmentProgressModel.currentTreatmentMonitoring.id);
                _local2 = (this.worldModel.getRoomOfActor(_local1.id) as IRoom);
                dispatch(new InfoEvent(EventConstants.HIGHLIGHT_ACTOR, _local2.machine.id));
            };
        }
        private function onViewRollOver(_arg1:MouseEvent):void{
            this.view.select();
            var _local2:String = (this._currentToolTipTextPointer) ? this.languageModel.getTextByKey(this._currentToolTipTextPointer) : null;
            this.view.setTooltipData(_local2);
        }
        private function onViewRollOut(_arg1:MouseEvent):void{
            this.view.deselect();
            this.view.setTooltipData(CLEAR_TOOL_TIP);
        }
        private function destroy():void{
            dispatch(new TickingEvent(TickingEvent.REMOVE_TICKING, this));
        }
        override public function preRemove():void{
            this.destroy();
            eventMap.unmapListeners();
            super.onRemove();
        }

    }
}//package net.wooga.pets.views 
